package wang.controller;

import wang.bean.Theme;
import wang.dao.ThemeDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@WebServlet(name = "ThemeController", value = "*.theme")
public class ThemeController extends HttpServlet {
  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    PrintWriter out = response.getWriter();
    HttpSession session = request.getSession();
    // 编写正则表达式，匹配以 / 开头，后面跟任意数量的任意字符（除了换行符），最后是 .theme
    String regex = "/(\\w+)\\.theme";
    // 编译正则表达式，创建Matcher对象
    String servletPath = request.getServletPath();
    Matcher matcher = Pattern.compile(regex).matcher(servletPath);
    if (!matcher.matches()) {
      return;
    }
    String requestUrl = matcher.group(1);
    ThemeDao themeDao = new ThemeDao();
    switch (requestUrl) {
      case "insert": {
        String name = request.getParameter("name");
        Theme target = themeDao.findByName(name);
        if (target != null) {
          out.write("<script>alert('主题已存在……');location.href='admin_tourist_attraction.jsp';</script>");
        } else {
          List<Theme> list = themeDao.list();
          if (list.size() >= 5) {
            out.write("<script>alert('主题数量已达上限……');location.href='admin_tourist_attraction.jsp';</script>");
          }
          Theme theme = new Theme();
          theme.setName(name);
          themeDao.insert(theme);
          out.write("<script>alert('添加成功……');location.href='admin_tourist_attraction.jsp';</script>");
          // 刷新当前页
        }
        break;
      }
      case "update": {
        // 更新主题
        int id = Integer.parseInt(request.getParameter("id"));
        String name = request.getParameter("name");
        Theme byId = themeDao.get(id);
        // 先用id找分类，找不到报错
        if (byId == null) {
          out.println("<script>alert('该主题不存在，无法修改！');location.href='adminList.ancientPainting';</script>");
        } else {
          if (name.equals(byId.getName())) {
            out.write("<script>alert('更新成功……');location.href='admin_tourist_attraction.jsp';</script>");
          } else {
            byId.setName(name);
            // TODO 更新在用这个主题的景点
            themeDao.update(byId);
            out.write("<script>alert('更新成功……');location.href='admin_tourist_attraction.jsp';</script>");
          }
        }
        break;
      }
      case "delete": {
        // 删除主题
        int id = Integer.parseInt(request.getParameter("id"));
        // TODO 判断是否有景点在用这个主题
        themeDao.delete(id);
        out.write("<script>alert('删除成功……');location.href='admin_tourist_attraction.jsp';</script>");
        break;
      }
    }
  }

  @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    this.doGet(request, response);
  }
}
